package com.yantong.controller.statistics;

import com.yantong.pojo.vo.*;
import com.yantong.service.CategoryService;
import com.yantong.pojo.*;
import com.yantong.service.CentreService;
import com.yantong.constant.PermisFlag;
import com.yantong.controller.BaseController;
import com.yantong.utils.DateUtils;
import com.yantong.utils.PageUtils;
import com.yantong.utils.excel.ExcelHelper;
import com.yantong.service.ReportService;
import com.yantong.service.CommanderListService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/report/manage")
public class ReportController extends BaseController {
	private static final String prefix = "report";

	private static final Logger logger = LoggerFactory.getLogger(ReportController.class);
	@Autowired
	private ReportService reportService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private CentreService centreService;
	 @Autowired
	private CommanderListService commanderListService;

	/**
	 * 跳转单品销售统计页面
	 * 
	 * @author baolingyu
	 * @date 2018年9月17日-下午3:00:45
	 */
	@GetMapping("/toSingleSale")
	@RequiresPermissions("report:manage:singleSale")
	public String toSingleSale(Model model) {
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/singlesale";
	}

	/**
	 * 单品销售情况列表
	 * 
	 * @param offset
	 * @param limit
	 * @return
	 * @author baolingyu
	 * @date 2018年9月17日-下午3:05:10
	 */
	@GetMapping("/singleSale")
	@ResponseBody
	@RequiresPermissions("report:manage:singleSale")
	public PageUtils<ReportSingleSaleVo> reportSingleSale(@RequestParam HashMap<String, Object> map, int offset,
                                                          int limit) {
		// HashMap<String, Object> map = new HashMap<>();
		StringBuilder time = new StringBuilder("");
		if (StringUtils.isNotBlank(map.get("year").toString())) {
			time.append("%Y-%m");
		}
		if (StringUtils.isNotBlank(map.get("month").toString())) {
			time.append("-%d");
		}
		if (StringUtils.isNotBlank(map.get("day").toString())) {
			time.append(" %H时");
		}
		map.put("timeFormat", time.toString());
		map.put("teamId",getUserTeamId());
		return reportService.reportSingleSale(map, offset, limit);
	}

	@GetMapping("/toProductSale")
	@RequiresPermissions("report:manage:productSale")
	public String toProductSale(Model model) {
		 ListMemberParams listMemberParams = new ListMemberParams();
	     if(getUserCentreId()!=null) {
		      listMemberParams.setCentreId(getUserCentreId());
	    }
	    List<Member> teams = commanderListService.listCommanders(listMemberParams , new Member());
		model.addAttribute("teams",teams);
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/productsale";
	}
	
	/**
	 * 商城订单统计
	 * @param model
	 * @return
	 */
	@GetMapping("/toMallStatic")
	@RequiresPermissions("report:manage:mallStatic")
	public String toMailStatics(Model model) {
		model.addAllAttributes(getCentreIdModelInfo());
		model.addAttribute("cjje_endDate", DateUtils.format(new Date()));
		model.addAttribute("yuan_endDate", DateUtils.format(new Date()));
		model.addAttribute("order_startDate", "2019-01-01");
		model.addAttribute("order_endDate", DateUtils.format(new Date()));
		return prefix + "/mallstatic";
	}

	/**
	 * 商品销售明细
	 *
	 * @author baolingyu
	 * @date 2018年9月14日-下午8:48:56
	 */
	@GetMapping("/productSale")
	@ResponseBody
	@RequiresPermissions("report:manage:productSale")
	public PageUtils<ReportProductSaleVo> reportProductSale(@RequestParam HashMap<String, Object> map, int offset,
                                                            int limit) {
		String column = map.get("column").toString();
		if (StringUtils.isNotBlank(column)) {
			map.put("column", "order by b." + column + " desc");
		}
		map.put("liansuoid",getUserTeamId());
		return reportService.reportProductSale(map, offset, limit);
	}
	
	@GetMapping("/exportProductSale")
	public void exportProductSale(@RequestParam HashMap<String, Object> map,
			HttpServletResponse response) {
		String column = map.get("column").toString();
		if (StringUtils.isNotBlank(column)) {
			map.put("column", "order by b." + column + " desc");
		}
		List<List<Object>> resultData = new ArrayList<>();
		String[] head = { "订单号", "商品名称","规格名称", "供应商","数量", "价格","门店佣金", "付款时间","下单人", "下单人ID","下单人电话","门店姓名", "小区" };
		resultData.add(0, Arrays.asList(head));

		List<ReportProductSaleVo> list = reportService.reportProductSale(map);
		if (CollectionUtils.isNotEmpty(list)) {
			for (ReportProductSaleVo obj : list) {
				if (obj == null) {
					continue;
				}
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				List<Object> row = new ArrayList<>();
				row.add(obj.getOrdersn());
				row.add(obj.getProductName());
				row.add(obj.getSupplyName());
				row.add(obj.getSupplyName());
				row.add(obj.getTotal());
				row.add(obj.getPrice());
				row.add(obj.getCommision1());
				row.add(obj.getPayTime() == null ? "" : sdf.format(obj.getPayTime()));
				row.add(obj.getNickName());
				row.add(obj.getMid()==null?"":obj.getMid());
				row.add(obj.getPhone());
				row.add(obj.gettNickname());
				row.add(obj.gettXiaoqu());
				resultData.add(row);
			}
		}
		String fileName = "商品销售明细_" + DateUtils.format(new Date(), "yyyyMMddHHmmss");
		try {
			ExcelHelper.exportExcel(response, fileName, "商品销售明细统计", resultData);
		} catch (IOException e) {
			logger.info("export achieve count error:{}", e.getMessage());
		}
	}
	
	@GetMapping("/productSaleZj")
	@ResponseBody
	@RequiresPermissions("report:manage:productSale")
	public Long productSaleZj (@RequestParam HashMap<String, Object> map) {
		map.put("liansuoid",getUserTeamId());
		return reportService.productsaleZj(map);
	}
	
	/**
	 * 跳转商品销量统计列表页面
	 * 
	 * @return
	 * @author baolingyu
	 * @date 2018年9月16日-上午9:45:28
	 */
	@GetMapping("/toProductSaleList")
	@RequiresPermissions("report:manage:productSaleList")
	public String toProductSaleList(Model model) {
		ListMemberParams listMemberParams = new ListMemberParams();
	     if(getUserCentreId()!=null) {
		      listMemberParams.setCentreId(getUserCentreId());
	    }
	    List<Member> teams = commanderListService.listCommanders(listMemberParams , new Member());
		model.addAttribute("teams",teams);
		model.addAttribute("category", categoryService.listCategorys(0L));
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/productsale_list";
	}

	/**
	 * 商品销售统计列表数据
	 * 
	 * @param offset
	 * @param limit
	 * @return
	 * @author baolingyu
	 * @date 2018年9月16日-上午9:46:36
	 */
	@GetMapping("/productSaleList")
	@ResponseBody
	@RequiresPermissions("report:manage:productSaleList")
	public PageUtils<ReportProductSaleListVo> productSaleList(@RequestParam HashMap<String, Object> map,
			int offset, int limit) {
		map.put("teamId",getUserTeamId());
		return reportService.reportProductSaleList(map, offset, limit);
	}

	/**
	 * 统计当前页面的订单数量和总金额
	 * @param map
	 * @param offset
	 * @param limit
	 * @return
	 */
	@GetMapping("/countProductSale")
	@ResponseBody
	@RequiresPermissions("report:manage:productSaleList")
	public Map<String,Object> countProductSale(@RequestParam HashMap<String, Object> map,
															  int offset, int limit) {
		map.put("teamId",getUserTeamId());
		return reportService.countProductSale(map, offset, limit);
	}
	
	@GetMapping("/exportProductSaleList")
	public void exportProductSaleList(@RequestParam HashMap<String, Object> map,
			HttpServletResponse response) {
		List<List<Object>> resultData = new ArrayList<>();
		String[] head = { "商品名称", "供应商","买手","站点","规格名称", "一级分类名称","二级分类名称", "售价", "门店佣金","推荐门店佣金", "成本价","平台成本价","平台毛利","销售数量", "销售金额","总毛利", "上架时间","商品区分" };
		resultData.add(0, Arrays.asList(head));

		List<ReportProductSaleListVo> list = reportService.reportProductSaleList(map);
		if (CollectionUtils.isNotEmpty(list)) {
			for (ReportProductSaleListVo obj : list) {
				if (obj == null) {
					continue;
				}
				String baseproductid = "";
				
					if(obj.getIsKuaidi() != null && obj.getIsKuaidi()==1) {
					  baseproductid = "快递直发";
					}else {
						if(obj.getBaseproductid()!=null && !obj.getBaseproductid().equals("")) {
							baseproductid = "平台商品";
						}else {
							baseproductid = "商户商品";
						}
				}	
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				List<Object> row = new ArrayList<>();
				row.add(obj.getProductName());
				row.add(obj.getSupplyName());
				row.add(StringUtils.isNotEmpty(obj.getBuyrealnm())?obj.getBuyrealnm():"未绑定买手");
				row.add(obj.getName());
				row.add(obj.getOptionname());
				row.add(obj.getPcateName());
				row.add(obj.getCcateName());
				row.add(obj.getMarketprice());
				row.add(obj.getHeadcommission());
				row.add(obj.getSuperiorcommission());
				row.add(obj.getCostprice());
				if(PermisFlag.FLAG_PLATFORM.equals(getUserFlag())) {
					row.add(obj.getBasecostprice());
				}else {
					row.add("你无权限查看");
				}
				if(PermisFlag.FLAG_PLATFORM.equals(getUserFlag())) {
					row.add(obj.getProfit());
				}else {
					row.add("你无权限查看");
				}
				row.add(obj.getSumTotal());
				row.add(obj.getSumPrice());
				if(PermisFlag.FLAG_PLATFORM.equals(getUserFlag())) {
					row.add(obj.getSumprofit());
				}else {
					row.add("你无权限查看");
				}
				row.add(obj.getUpStartTime() == null ? "" : sdf.format(obj.getUpStartTime()));
				row.add(baseproductid);
				resultData.add(row);
			}
		}
		String fileName = "商品销售统计_" + DateUtils.format(new Date(), "yyyyMMddHHmmss");
		try {
			ExcelHelper.exportExcel(response, fileName, "商品销售统计", resultData);
		} catch (IOException e) {
			logger.info("export achieve count error:{}", e.getMessage());
		}
	}

	/**
	 * 跳转门店销量统计页面
	 * 
	 * @return
	 * @author baolingyu
	 * @date 2018年9月16日-上午9:45:28
	 */
	@GetMapping("/toMemberSale")
	@RequiresPermissions("report:manage:memberSale")
	public String toMemberSale(Model model) {
		// model.addAttribute("category", categoryService.listCategorys(0L));
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/membersale";
	}

	/**
	 * 门店销量情况
	 * 
	 * @param offset
	 * @param limit
	 * @return
	 * @author baolingyu
	 * @date 2018年9月16日-下午6:12:08
	 */
	@GetMapping("/memberSale")
	@ResponseBody
	@RequiresPermissions("report:manage:memberSale")
	public PageUtils<ReportMemberSaleVo> memberSale(@RequestParam HashMap<String, Object> map, int offset, int limit) {
		map.put("teamId",getUserTeamId());
		return reportService.reportMemberSale(map, offset, limit);
	}
	
	@GetMapping("/exportMemberSale")
	public void exportMemberSale(@RequestParam HashMap<String, Object> map,
			HttpServletResponse response) {
		List<List<Object>> resultData = new ArrayList<>();
		String[] head = { "门店姓名", "门店电话","开通时间","门店名称", "所属线路","会员数量", "订单数量", "门店推荐人","推荐人电话","所占比例(%)","商品数量", "所占比例(%)","交易额", "所占比例(%)" };
		resultData.add(0, Arrays.asList(head));

		List<ReportMemberSaleVo> list = reportService.reportMemberSale(map);
		if (CollectionUtils.isNotEmpty(list)) {
			for (ReportMemberSaleVo obj : list) {
				if (obj == null) {
					continue;
				}
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				List<Object> row = new ArrayList<>();
				row.add(obj.getRealname());
				row.add(obj.getMobile());
				row.add(obj.getsTime() == null ? "" : sdf.format(obj.getsTime()).substring(0,10));
				row.add(obj.gettXiaoquName());
				row.add(obj.getUsername());
				row.add(obj.getPeopleNumber());
				row.add(obj.getSumOrder());
				row.add(obj.getUpRealName());
				row.add(obj.getUpMobile());
				row.add(obj.getOrderRate());
				row.add(obj.getSumProduct());
				row.add(obj.getProductRate());
				row.add(obj.getSumPrice());
				row.add(obj.getPriceRate());
				resultData.add(row);
			}
		}
		String fileName = "门店订单销售统计_" + DateUtils.format(new Date(), "yyyyMMddHHmmss");
		try {
			ExcelHelper.exportExcel(response, fileName, "门店订单销售统计", resultData);
		} catch (IOException e) {
			logger.info("export achieve count error:{}", e.getMessage());
		}
	}

	/**
	 * 跳转门店退货统计页面
	 * 
	 * @return
	 * @author baolingyu
	 * @date 2018年9月16日-上午9:45:28
	 */
	@GetMapping("/toMemberRefund")
	@RequiresPermissions("report:manage:memberRefund")
	public String toMemberRefund(Model model) {
		// model.addAttribute("category", categoryService.listCategorys(0L));
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/memberrefund";
	}

	/**
	 * 门店退货统计
	 * 
	 * @param offset
	 * @param limit
	 * @return
	 * @author baolingyu
	 * @date 2018年9月16日-下午6:12:08
	 */
	@GetMapping("/memberRefund")
	@ResponseBody
	@RequiresPermissions("report:manage:memberRefund")
	public PageUtils<ReportMemberRefundVo> memberRefund(@RequestParam HashMap<String, Object> map, int offset,
                                                        int limit) {
		if (StringUtils.isBlank(map.get("column").toString())) {
			map.put("column", "sumPrice");
		}
		return reportService.reportMemberRefund(map, offset, limit);
	}
	
	/**
	 * 门店退货统计导出
	 * @param map
	 * @param response
	 * @author baolingyu
	 * @date 2018年11月19日-下午10:10:08
	 */
	@GetMapping("/exportMemberRefund")
	public void exportMemberRefund(@RequestParam HashMap<String, Object> map,
			HttpServletResponse response) {
		List<List<Object>> resultData = new ArrayList<>();
		String[] head = { "门店姓名", "门店名称", "退单数量", "所占比例(%)", "退款金额", "所占比例(%)" };
		resultData.add(0, Arrays.asList(head));

		List<ReportMemberRefundVo> list = (List<ReportMemberRefundVo>) reportService.reportMemberRefund(map,1, 1).getRows();
		if (CollectionUtils.isNotEmpty(list)) {
			for (ReportMemberRefundVo rtv : list) {
				if (rtv == null) {
					continue;
				}
				List<Object> row = new ArrayList<>();
				row.add(rtv.getNickname());
				row.add(rtv.gettXiaoquName());
				row.add(rtv.getCount());
				row.add(rtv.getCountRate());
				row.add(rtv.getSumPrice());
				row.add(rtv.getPriceRate());
				resultData.add(row);
			}
		}
		String fileName = "门店退货统计_" + DateUtils.format(new Date(), "yyyyMMddHHmmss");
		try {
			ExcelHelper.exportExcel(response, fileName, "门店退货统计", resultData);
		} catch (IOException e) {
			logger.info("export achieve count error:{}", e.getMessage());
		}
	}
	
	@GetMapping("/getZj")
	@ResponseBody
	@RequiresPermissions("report:manage:reportZj")
	public ReportTotalVo getZj() {
		return null;
	}

	/**
	 * 门店业绩统计
	 * 
	 * @param model
	 * @return
	 */
	@GetMapping("/achieveCount")
	@RequiresPermissions("report:achieveCount:achieveCount")
	public String tmAchieve(Model model) {
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/achieveCount";
	}

	/**
	 * 门店业绩统计
	 * 
	 * @param centreId
	 * @param province
	 * @param city
	 * @param queryItem
	 * @param offset
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@GetMapping("/achieveCountList")
	@RequiresPermissions("report:achieveCount:achieveCount")
	public PageUtils<ReportTmAchieveVo> listTmAchieve(Long centreId, String province, String city, String queryItem,
			int offset, int limit) {
		Map<String, Object> params = new HashMap<>(4);
		if (centreId != null) {
			params.put("centreId", centreId);
		}
		Long teamId = getUserTeamId();
		if ( teamId != null) {
			params.put("teamId", teamId);
		}
		if (StringUtils.isNotEmpty(province)) {
			params.put("province", province);
		}
		if (StringUtils.isNotEmpty(city)) {
			params.put("city", city);
		}
		if (StringUtils.isNotEmpty(queryItem)) {
			params.put("queryItem", queryItem);
		}
		return reportService.tmAchieve(params, offset, limit);
	}

	@GetMapping("/export")
	@RequiresPermissions("report:achieveCount:export")
	public void exportTmAchieve(Long centreId, String province, String city, String queryItem,
			HttpServletResponse response) {
		Map<String, Object> params = new HashMap<>(4);
		if (centreId != null) {
			params.put("centreId", centreId);
		}
		if (StringUtils.isNotEmpty(province)) {
			params.put("province", province);
		}
		if (StringUtils.isNotEmpty(city)) {
			params.put("city", city);
		}
		if (StringUtils.isNotEmpty(queryItem)) {
			params.put("queryItem", queryItem);
		}
		List<List<Object>> resultData = new ArrayList<>();
		String[] head = { "昵称", "姓名", "门店名称", "团队业绩金额", "团队订单总数", "兑率", "佣金", "统计日期" };
		resultData.add(0, Arrays.asList(head));

		List<ReportTmAchieveVo> list = reportService.tmAchieve(params);
		if (CollectionUtils.isNotEmpty(list)) {
			for (ReportTmAchieveVo rtv : list) {
				if (rtv == null) {
					continue;
				}
				List<Object> row = new ArrayList<>();
				row.add(rtv.getNickname());
				row.add(rtv.getRealname());
				row.add(rtv.gettXiaoqu());
				row.add(rtv.getAchieve());
				row.add(rtv.getOrderTotal());
				row.add(rtv.getRate());
				row.add(rtv.getCommission());
				row.add(rtv.getMonth());
				resultData.add(row);
			}
		}
		String fileName = "门店团队业绩_" + DateUtils.format(new Date(), "yyyyMMddHHmmss");
		try {
			ExcelHelper.exportExcel(response, fileName, "门店团队业绩", resultData);
		} catch (IOException e) {
			logger.info("export achieve count error:{}", e.getMessage());
		}
	}
	
	/**
	 * 跳转到商品库商品销售情况
	 * 
	 * @author baolingyu
	 * @date 2018年9月17日-下午3:00:45
	 */
	@GetMapping("/toProductbaseSale")
	@RequiresPermissions("report:manage:productbaseSale")
	public String toProductbaseSale(Model model) {
		 ListMemberParams listMemberParams = new ListMemberParams();
	     if(getUserCentreId()!=null) {
		      listMemberParams.setCentreId(getUserCentreId());
	    }
	    List<Member> teams = commanderListService.listCommanders(listMemberParams , new Member());
		model.addAttribute("teams",teams);
		model.addAttribute("category", categoryService.listCategorys(0L));
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/productbaseSale";
	}

	/**
	 * 商品库商品销售情况
	 * 
	 * @param offset
	 * @param limit
	 * @return
	 * @author baolingyu
	 * @date 2018年9月17日-下午3:05:10
	 */
	@GetMapping("/productbaseSale")
	@ResponseBody
	@RequiresPermissions("report:manage:productbaseSale")
	public PageUtils<ReportProductSaleListVo> reportProductbaseSale(@RequestParam HashMap<String, Object> map, int offset,
                                                                    int limit) {
		return reportService.reportProductbaseSale(map, offset, limit);
	}

	/**
	 * 商品库商品销售总计
	 *
	 * @return
	 */
	@GetMapping("/productTotalbaseSale")
	@ResponseBody
	@RequiresPermissions("report:manage:productbaseSale")
	public Map<String, Object> productTotalbaseSale(@RequestParam HashMap<String, Object> map) {
		Map<String, Object> countTotalProductbaseSale = reportService.countTotalProductbaseSale(map);
		if(countTotalProductbaseSale==null){
			countTotalProductbaseSale=new HashMap<>();
		}
		return countTotalProductbaseSale;
	}

	@GetMapping("/exportProductbaseSale")
	public void exportProductbaseSale(@RequestParam HashMap<String, Object> map, HttpServletResponse response) {
		List<List<Object>> resultData = new ArrayList<>();
		String[] head = new String[]{"商品名称", "一级分类", "二级分类", "供应商","买手", "销售价", "门店佣金", "推荐佣金", "站点成本价","平台成本价", "销售数量", "销售金额","规格名称","上架时间"};
		resultData.add(0, Arrays.asList(head));
		List<ReportProductSaleListVo> list = reportService.reportProductbaseSale(map);
		if (CollectionUtils.isNotEmpty(list)) {
			for (ReportProductSaleListVo rtv : list) {
				if (rtv == null) {
					continue;
				}
				List<Object> row = new ArrayList<>();
				row.add(rtv.getProductName());
				row.add(rtv.getPcateName());
				row.add(rtv.getCcateName());
				row.add(rtv.getSupplyName());
				row.add(StringUtils.isNotEmpty(rtv.getBuyrealnm())?rtv.getBuyrealnm():"未绑定买手");
				row.add(rtv.getMarketprice());
				row.add(rtv.getHeadcommission());
				row.add(rtv.getSuperiorcommission());
				row.add(rtv.getCostprice());
				row.add(rtv.getBcostprice());
				row.add(rtv.getSumTotal());
				row.add(rtv.getSumPrice());
				row.add(rtv.getOptionname());
				if(rtv.getUpStartTime()!=null){
					row.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(rtv.getUpStartTime()));
				}else{
					row.add(rtv.getUpStartTime());
				}
				//如果商户Id为空则查询全部商户
				if(map.get("centreId") == ""){
					HashMap<String, Object> detailMap = map;
					//根据商品查询服务区销量
					detailMap.put("productId",rtv.getProductId());
					detailMap.put("skuId",rtv.getSkuId());
					detailMap.remove("supplyName");
					List<ReportTmAchieveVo> reportProduct=reportService.reportProductSaleDetail(detailMap);
					List<Centre> listCentre=centreService.listAllCentres();
					if(reportProduct.size()>0){
						for (Centre centre : listCentre) {
							//定义个变量判断遍历的商户是否有值 0默认没有 1有
							int variate =0;
							for (ReportTmAchieveVo reportTmAchieveVo : reportProduct) {
								if(centre.getName().equals(reportTmAchieveVo.getName())){
									row.add(centre.getName()+":"+reportTmAchieveVo.getOrderTotal());
									variate=1;
									break;
								}
							}
							if(variate==0){
								row.add(centre.getName()+":"+0);
							}
						}
					}
				}
				resultData.add(row);
			}
		}
		String fileName = "商品库商品销量统计列表" + DateUtils.format(new Date(), "yyyyMMddHHmmss");
		try {
			ExcelHelper.exportExcel(response, fileName, "商品库商品销量统计列表", resultData);
		} catch (IOException e) {
			logger.info("export achieve count error:{}", e.getMessage());
		}
	}

	/**
	 *
	 * 跳转销售统计详情页面
	 *
	 * @author baolingyu
	 * @date 2018年9月17日-下午3:00:45
	 */
	@GetMapping("/saleDetail")
	@RequiresPermissions("report:manage:productSaleDetail")
	public String saleDetail(@RequestParam HashMap<String, Object> map, Model model) {
		map.remove("supplyName");
		model.addAttribute("producSale", reportService.reportProductSaleDetail(map));
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/saleDetail";
	}

	/**
	 * 门店对账管理
	 *
	 * @param model
	 * @return
	 */
	@GetMapping("/billCount")
	@RequiresPermissions("report:billCount:billCount")
	public String tmBillCount(Model model) {
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/billCount";
	}

	/**
	 * 门店对账管理
	 *
	 * @param centreId
	 * @param province
	 * @param city
	 * @param queryItem
	 * @param offset
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@GetMapping("/billCountList")
	@RequiresPermissions("report:billCount:billCount")
	public PageUtils<ReportTmBillVo> listtmBillCount(Long centreId, String province, String city, String queryItem,
													  int offset, int limit) {
		Map<String, Object> params = new HashMap<>(4);
		if (centreId != null) {
			params.put("centreId", centreId);
		}
		Long teamId = getUserTeamId();
		if ( teamId != null) {
			params.put("teamId", teamId);
		}
		if (StringUtils.isNotEmpty(province)) {
			params.put("province", province);
		}
		if (StringUtils.isNotEmpty(city)) {
			params.put("city", city);
		}
		if (StringUtils.isNotEmpty(queryItem)) {
			params.put("queryItem", queryItem);
		}
		return reportService.tmBill(params, offset, limit);
	}

	/**
	 * 导出商品成本利润表
	 *
	 * @param response
	 */
	@RequiresPermissions("report:billCount:export")
	@GetMapping("/billCountExport")
	public void billCountExport(HttpServletResponse response, Long centreId, String province, String city, String queryItem) {
		Map<String, Object> params = new HashMap<>(4);
		if (centreId != null) {
			params.put("centreId", centreId);
		}
		Long teamId = getUserTeamId();
		if ( teamId != null) {
			params.put("teamId", teamId);
		}
		if (StringUtils.isNotEmpty(province)) {
			params.put("province", province);
		}
		if (StringUtils.isNotEmpty(city)) {
			params.put("city", city);
		}
		if (StringUtils.isNotEmpty(queryItem)) {
			params.put("queryItem", queryItem);
		}
		List<ReportTmBillVo> billList = reportService.getAllBillList(params);

		String[] head = {"序号", "商户", "门店", "统计月份", "应结算金额", "订单总量", "订单总金额", "支付总金额",
				"积分抵扣总额", "满减抵扣总额", "商户优惠券抵扣金额", "平台优惠券抵扣金额", "门店优惠券抵扣金额", "门店佣金",
				"门店推荐人佣金", "商户推荐人佣金", "商户代理佣金", "推荐人佣金", "二级推荐人佣金", "用户代理佣金", "店员佣金"};
		List<List<Object>> resultData = new ArrayList<>();
		resultData.add(0, Arrays.asList(head));

		if(billList != null && billList.size() > 0) {
			int i = 1;
			for (ReportTmBillVo entity : billList) {
				List<Object> row = new ArrayList<>();
				row.add(i);//序号
				row.add(entity.getCentreName());//商户
				row.add(entity.getTeamName());//门店
				row.add(entity.getMonth());//统计月份
				row.add(entity.getSettlementPrice());//应结算金额
				row.add(entity.getOrderTotal());//订单总量
				row.add(entity.getTotalOrderPrice());//订单总金额
				row.add(entity.getTotalPayPrice());//支付总金额
				row.add(entity.getTotalDeductPrice());//积分抵扣总额
				row.add(entity.getTotalFullDiscountPrice());//满减抵扣总额
				row.add(entity.getCentreCouponDeductTotal());//商户优惠券抵扣金额
				row.add(entity.getPlatformCouponDeductTotal());//平台优惠券抵扣金额
				row.add(entity.getTeamCouponDeductTotal());//门店优惠券抵扣金额
				row.add(entity.getTotalCommision1());//门店佣金
				row.add(entity.getTotalCommision2());//门店推荐人佣金
				row.add(entity.getTotalCommision3());//商户推荐人佣金
				row.add(entity.getTotalCommision4());//商户代理佣金
				row.add(entity.getTotalCommision5());//推荐人佣金
				row.add(entity.getTotalCommision6());//二级推荐人佣金
				row.add(entity.getTotalCommision7());//用户代理佣金
				row.add(entity.getTotalCommision8());//店员佣金
				resultData.add(row);
				i++;
			}
		}
		String fileName = "门店对账" + DateUtils.format(new Date(), "yyyy/MM/dd/ HH:mm:ss");
		try {
			ExcelHelper.exportExcel(response, fileName, "门店对账", resultData);
		} catch (IOException e) {
			logger.info("export member error:{}", e.getMessage());
		}

	}
}
